package com.common.baselibrary.imagepicker.util;

import android.os.Process;

import com.common.baselibrary.base.BaseActivity;
import com.common.baselibrary.util.LogHelper;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * create by xuyougen.
 */
public class ActivityStackManager {
    private static final List<WeakReference<BaseActivity>> activityList = new ArrayList<>();

    public static void add(WeakReference<BaseActivity> weakReference) {
        LogHelper.e("ActivityStackManager-add", weakReference.get().getClass().getSimpleName());
        activityList.add(weakReference);
    }

    public static void remove(WeakReference<BaseActivity> weakReference) {
        LogHelper.e("ActivityStackManager-remove", weakReference.get().getClass().getSimpleName());
        activityList.remove(weakReference);
    }


    /**
     * 是否有某个类的实例
     */
    public static boolean hasInstanceOf(Class<? extends BaseActivity> clazz) {
        for (WeakReference<BaseActivity> reference : activityList) {
            BaseActivity activity = reference.get();
            if (activity != null) {
                if (clazz.equals(activity.getClass()))
                    return true;
            }
        }
        return false;
    }


    /**
     * 是否在栈顶
     *
     * @param currentActivity 需要判断的 Activity
     * @return true 在栈顶
     */
    public static boolean isOnStackTop(BaseActivity currentActivity) {
        WeakReference<BaseActivity> reference = activityList.get(activityList.size() - 1);
        BaseActivity topActivity = reference.get();
        if (topActivity == null) return false;
        return topActivity == currentActivity;
    }

    /**
     * 从栈顶开始关闭 Activity 直到某个 class 为止
     *
     * @param clazz 目标 Activity class
     */
    public static void closeActivitiesFromStackTopUntilTheClass(Class<? extends BaseActivity> clazz) {
        for (int index = activityList.size() - 1; index >= 0; index--) {
            WeakReference<BaseActivity> reference = activityList.get(index);
            BaseActivity activity = reference.get();
            if (activity != null) {
                if (activity.getClass().equals(clazz)) {
                    break;
                }
                activity.finish();
            }
        }
    }


    /**
     * 从栈顶开始，关闭所传入 class 对应生成的 Activity 实例
     *
     * @param justOnce 同一个 class 是否只关闭 一个实例（靠近栈顶的）
     * @param classes  要关闭的 Activity 的 class
     */
    @SafeVarargs
    public static void closeActivitiesFromStackTop(boolean justOnce, Class<? extends BaseActivity>... classes) {
        for (Class<? extends BaseActivity> clazz : classes) {
            for (int index = activityList.size() - 1; index >= 0; index--) {
                WeakReference<BaseActivity> reference = activityList.get(index);
                BaseActivity activity = reference.get();
                if (activity != null) {
                    if (activity.getClass().equals(clazz)) {
                        activity.finish();
                        if (justOnce) {
                            break;
                        }
                    }
                }
            }
        }
    }

    /**
     * 结束所有的Activity
     */
    public static void closeAllActivities() {
        for (WeakReference<BaseActivity> baseActivityWeakReference : activityList) {
            BaseActivity baseActivity = baseActivityWeakReference.get();
            if (baseActivity != null) {
                baseActivity.finish();
            }
        }
    }

    /**
     * 结束所有的Activity，除了。。。传入的class类型
     */
    public static void closeAllActivitiesExceptClass(Class<? extends BaseActivity> clazz) {
        for (WeakReference<BaseActivity> baseActivityWeakReference : activityList) {
            BaseActivity baseActivity = baseActivityWeakReference.get();
            if (baseActivity != null) {
                if (baseActivity.getClass().equals(clazz)) {
                    continue;
                }
                baseActivity.finish();
            }
        }
    }

    /**
     * 强制退出应用程序
     */
    public static void forceExitApplication() {
        closeAllActivities();
        Process.killProcess(Process.myPid());
    }
}
